/**
 *
 * 版权所有 © cybersky.com
 *
 */
package cn.cybertron.primus.report.custom;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

import org.nutz.ioc.Ioc;
import org.nutz.mvc.NutConfig;

import cn.cybertron.primus.appservice.AbstractAppService;

/**
 * 自定义报表配置类，单例，由配置文件实例化
 * @author: ch
 */
public class ReportCustomConfig extends AbstractAppService {
	
	private static final ReportCustomConfig me = new ReportCustomConfig();
	
	public static final ReportCustomConfig me() {
		return me;
	}
	
	private ReportCustomConfig() {}
	
	private Map<String, ReportCustomItem> customTypes = Collections.synchronizedMap(new LinkedHashMap<String, ReportCustomItem>());
	
	public Map<String, ReportCustomItem> getCustomTypes() {
		return customTypes;
	}

	public void setCustomTypes(Map<String, ReportCustomItem> customTypes) {
		this.customTypes = customTypes;
	}

	/**
	 * 注册自定义报表类型
	 * @param item
	 */
	public void registerCustomType(ReportCustomItem item){
		if(customTypes == null){
			customTypes = new LinkedHashMap<String, ReportCustomItem>();
		}
		if(item != null)
			customTypes.put(item.getType(), item);
	}
	
	/**
	 * 注销自定义报表类型
	 * @param type
	 * @return
	 */
	public ReportCustomItem unregisterCustomType(String type) {
		if(customTypes != null)
			return customTypes.remove(type);
		return null;
	}

	/**
	 * 获取支持的报表类型
	 */
	public String[] getSupportedCustomTypes() {
		String[] types = null;
		if(customTypes != null) {
			types = customTypes.keySet().toArray(new String[customTypes.size()]);
		}
		return types;		
	}
	
	/**
	 * 获取指定类型报表的添加步骤
	 * @param type
	 * @return
	 */
	public ReportCustomItem getCustomItem(String type) {
		if(customTypes != null)
			return customTypes.get(type);
		return null;
	}
	
	/**
	 * 初始化自定义报表配置
	 */
	@Override
	public void init(NutConfig config) {
		Ioc ioc = config.getIoc();
		String[] names = ioc.getNamesByType(ReportCustomItem.class);
		for (String name : names) {
			if (name.startsWith(ReportCustomItem.IOC_NAME_PREFIX)) {
				registerCustomType(ioc.get(ReportCustomItem.class, name));
			}
		}
	}

	@Override
	public void destroy(NutConfig config) {
		
	}

}
